package br.com.dev.tcc.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.sinistro.Condutor;
import br.com.dev.tcc.bean.sinistro.NivelAgua;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.bean.sinistro.Vitima;

public class EnviaSinistroDialog extends Dialog {

	private OnConfirmListener listener;
	protected Sinistro sinistro;

	public EnviaSinistroDialog(Context context, Sinistro sinistro,
			OnConfirmListener listener) {

		this(context);
		this.sinistro = sinistro;
		this.listener = listener;

		setListeners();
		setFields();
	}

	private EnviaSinistroDialog(Context context) {

		super(context, R.style.DialogAppTheme);

		super.getWindow();
		super.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.setContentView(R.layout.envia_sinistro_dialog);
	}

	private void setFields() {

		((TextView) findViewById(R.id.txtValueOcorrencia)).setText(sinistro
				.getTipo().getStringId());
		((TextView) findViewById(R.id.txtValorData))
				.setText(sinistro.getData());

		Condutor condutor = sinistro.getCondutor();
		if (condutor != null) {
			findViewById(R.id.layoutCondutor).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.txtValueCondutor)).setText(condutor
					.toString());
			if (condutor.equals(Condutor.DEPENDENTE)) {
				TextView txtCpf = (TextView) findViewById(R.id.tvCpf);
				txtCpf.setVisibility(View.VISIBLE);
				txtCpf = (TextView) findViewById(R.id.txtValueCpf);
				txtCpf.setVisibility(View.VISIBLE);
				txtCpf.setText(sinistro.getDependente().getCpf());
			} else if (condutor.equals(Condutor.TERCEIRO)) {
				TextView txtNome = (TextView) findViewById(R.id.txtValueNome);
				txtNome.setVisibility(View.VISIBLE);
				txtNome.setText(sinistro.getTerceiro().getNome());
				TextView txtCpf = (TextView) findViewById(R.id.tvCpf);
				txtCpf.setVisibility(View.VISIBLE);
				txtCpf = (TextView) findViewById(R.id.txtValueCpf);
				txtCpf.setVisibility(View.VISIBLE);
				txtCpf.setText(sinistro.getTerceiro().getCpf());
			}
		}

		((TextView) findViewById(R.id.txtValorEndereco)).setText(sinistro
				.getLocal());
		((TextView) findViewById(R.id.txtValorDescritivo)).setText(sinistro
				.getDescritivo());

		Vitima vitima = sinistro.getVitima();
		if (vitima != null) {
			String nome = vitima.getNome();
			if (nome != null) {
				findViewById(R.id.layoutVitima).setVisibility(View.VISIBLE);
				((TextView) findViewById(R.id.txtValorNomeVitima))
						.setText(nome);
				String cpf = vitima.getCpf();
				((TextView) findViewById(R.id.txtValorCpfVitima))
						.setText(cpf != null ? cpf : getContext().getString(
								R.string.nao_informado));
			}
		}

		String bo = sinistro.getBo();
		if (bo != null) {
			findViewById(R.id.layoutBo).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.txtNumBo)).setText(bo);
		}

		NivelAgua nivelAgua = sinistro.getNivelAgua();
		if (nivelAgua != null) {
			findViewById(R.id.layoutNivelAgua).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.txtValorNivelAgua)).setText(nivelAgua
					.getDescricao());
		}
	}

	private void setListeners() {

		findViewById(R.id.btConfirm).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						dismiss();
						listener.onConfirm();
					}
				});

		findViewById(R.id.btBack).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						dismiss();
					}
				});
	}

	public void setListener(OnConfirmListener listener) {

		this.listener = listener;
	}

	public interface OnConfirmListener {

		public void onConfirm();
	}
}
